package controllers.caterinaController.controllers;

import java.util.ArrayList;

import person.Customer;
import budget.Request;
import controllers.Checks;
import controllers.ExceptionsTypes;
import controllers.Permission;
import controllers.PermissionList;
import controllers.dbWrapper.RequestWrapper;

public class RequestManager {
	public static ArrayList<Request> getAllRequest() throws Exception{
		PermissionList per = LoginManager.getPermission();
		if(per.can(Permission.REQUESTS_VIEW_ALL))
			return RequestWrapper.getAllRequest(true);
		else
			throw new Exception(ExceptionsTypes.PERMISSION_DENIDED.name());
	}
	
	public static ArrayList<Request> getRequest(Customer customer) throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		return RequestWrapper.getRequest(customer, false);
	}
	
	public static Request getRequest(long id) throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		return RequestWrapper.getRequest(id, true);
	}
	
	public static ArrayList<Request> getRequestBySearchFilter(String filter) throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		ArrayList<Request> all = RequestWrapper.getAllRequest(true);
		ArrayList<Request> filtered = new ArrayList<Request>();
		for(Request r : all)
			if(r.contains(filter))
				filtered.add(r);
		return filtered;
	}
	
	public static void registerRequest(Request request) throws Exception{		
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		Checks.checkRequest(request);
		RequestWrapper.insertRequest(true, request);
	}
	
	public static void removeRequest(Request request) throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		RequestWrapper.deleteRequest(true, request);
	}
}
